package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jluzh-electric/internal/api"
	"jluzh-electric/internal/config"
	electric "jluzh-electric/internal/source-api"
	"net/http"
	"time"
)

const DEFALUT_REFRESH_STATUS_MIN = 10 //刷新共用账号登录Cookie

func main() {
	//init config
	if config.Init() != nil {
		print("config \"dormitory_data.json\" file load fail! please check")
		return
	}

	//init electric source api
	loginErr := electric.GetElectricSessionInstance().Login() //api first login
	if loginErr != nil {
		fmt.Print(loginErr)
		fmt.Print("first login failed! please check network and env")
	}

	//Refresh login status time task
	ticker := time.Tick(DEFALUT_REFRESH_STATUS_MIN * time.Minute)
	go electric.GetElectricSessionInstance().RefreshStatus(ticker)

	//Start gin server
	server := gin.Default()

	//export api
	server.GET("/api/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})
	server.GET("/api/balance", api.DoGetElectricBalance)
	server.GET("/api/history", api.DoGetElectricHistory)
	server.GET("/api/recharge", api.DoGetRechargeHistory)

	err := server.Run()
	if err != nil {
		fmt.Println("Go Server Start Up Success!")
	}
}
